/*
* @(#)MaintForumGroupDetailAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.forum.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.forum.ForumOperationConstant;
import com.dcivision.forum.bean.ForumGroupDetail;
import com.dcivision.forum.core.ForumManager;
import com.dcivision.forum.dao.ForumGroupDetailDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractMaintAction;
/**
  MaintForumGroupDetailAction.java

  This class is for maint system user defined index.

    @author          Zoe Shum
    @company         DCIVision Limited
    @creation date   15/10/2003
    @version         $Revision: 1.14 $
    */

public class MaintForumGroupDetailAction extends AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.14 $";

  protected String listName = "";

  public MaintForumGroupDetailAction() {
    super();
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.forum.dao.ForumGroupDetailDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_DISCUSSION_FORUM);
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    MaintForumGroupDetailForm searchForm = (MaintForumGroupDetailForm) form;
    final String opMode = searchForm.getOpMode();
    log.debug("opMode is " + opMode);
    if (opMode != null) {
      
      ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
      if (forward!=null) {
        return forward;
      }
      if (GlobalConstant.NAV_MODE_REFRESH.equals(opMode)) {
        log.debug("enter refresh ");
        try {
          refreshFormData(mapping, searchForm, request, response);
        }
        catch (ApplicationException appEx) {
          this.rollback(request);
          handleApplicationException(request, appEx);
        }
        return mapping.findForward(GlobalConstant.NAV_MODE_REFRESH);

      }else if (ForumOperationConstant.FOURM_REPLY.equals(opMode)) {
         try {
           replyFormData(mapping, searchForm, request, response);
         }
         catch (ApplicationException appEx) {
           this.rollback(request);
           handleApplicationException(request, appEx);
         }
         return mapping.findForward(ForumOperationConstant.FOURM_REPLY);
       }
       else {
         return super.execute(mapping, form, request, response);
       }
    } else {
      return super.execute(mapping, form, request, response);
    }
  }

  public void refreshFormData(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    MaintForumGroupDetailForm ForumGroupDetailForm = (MaintForumGroupDetailForm) form;

    ForumGroupDetail ForumGroupDetail = (ForumGroupDetail) ForumGroupDetailForm.getFormData();

    //Set Header Form data
    String dummy_display_ObjectID = request.getParameter("dummy_display_ObjectID");
    request.setAttribute("dummy_display_ObjectID", dummy_display_ObjectID);

    ForumGroupDetailForm.setFormData(ForumGroupDetail);
    ForumGroupDetailForm.setTopic(ForumGroupDetail.getTopic());
    ForumGroupDetailForm.setDetail(ForumGroupDetail.getDetail());

  }

  public void replyFormData(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    MaintForumGroupDetailForm dmsDocDiscussionForm = (MaintForumGroupDetailForm) form;
    List resultList = new ArrayList();
    Integer ForumGroupDetailID = new Integer(dmsDocDiscussionForm.getID());
    ForumGroupDetailDAObject ForumGroupDetailDAO = new ForumGroupDetailDAObject(sessionContainer, conn);
    ForumGroupDetail ForumGroupDetail = (ForumGroupDetail)ForumGroupDetailDAO.getObjectByID(ForumGroupDetailID);
    resultList.add(ForumGroupDetail);
    request.setAttribute("selectedDiscussionList", new ArrayList(resultList));

    //Set Header Form data
    if (ForumGroupDetail.getParentID().intValue() == 0) {
      dmsDocDiscussionForm.setFormData(ForumGroupDetail);
      dmsDocDiscussionForm.setTopic(ForumGroupDetail.getTopic());
      dmsDocDiscussionForm.setDetail(ForumGroupDetail.getDetail());
      dmsDocDiscussionForm.setObjectID(TextUtility.formatIntegerObj(ForumGroupDetail.getObjectID()));
    }
    else {
      ForumGroupDetail masterTopicDiscussion = (ForumGroupDetail)ForumGroupDetailDAO.getObjectByID(ForumGroupDetail.getParentID());
      dmsDocDiscussionForm.setFormData(masterTopicDiscussion);
      dmsDocDiscussionForm.setTopic(masterTopicDiscussion.getTopic());
      dmsDocDiscussionForm.setDetail("");
      dmsDocDiscussionForm.setObjectID(TextUtility.formatIntegerObj(masterTopicDiscussion.getObjectID()));
    }
  }

  public void selectRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);

    MaintForumGroupDetailForm forumGroupDetailForm = (MaintForumGroupDetailForm) form;
    Integer ForumGroupDetailID = new Integer(forumGroupDetailForm.getID());
    String opMode = forumGroupDetailForm.getOpMode();
    String navMode = forumGroupDetailForm.getNavMode();

    ForumGroupDetailDAObject forumGroupDetailDAO = new ForumGroupDetailDAObject(sessionContainer, conn);
    ForumGroupDetail ForumGroupDetail = (ForumGroupDetail) forumGroupDetailDAO.getObjectByID(ForumGroupDetailID);
    forumGroupDetailForm.setFormData(ForumGroupDetail);
    forumGroupDetailForm.setTopic(ForumGroupDetail.getTopic());
    forumGroupDetailForm.setDetail(ForumGroupDetail.getDetail());
    List replyList = forumGroupDetailDAO.getReplyList(forumGroupDetailForm.getID(), forumGroupDetailForm.getObjectType(), forumGroupDetailForm.getObjectID());
    request.setAttribute("discussionReplyList", replyList);

    if (GlobalConstant.NAV_MODE_VIEW.equals(navMode)){
      //Update the Number of Read on Master Topic record
      int newRead = ForumGroupDetail.getReads().intValue() + 1;
      ForumGroupDetail.setReads(new Integer(newRead));
      ForumGroupDetail updatedDocDiscussion = (ForumGroupDetail) forumGroupDetailDAO.updateObject(ForumGroupDetail);
      this.commit(request);
    }


  }

  public void insertRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);

    List resultList = new ArrayList();
    Integer ParentID;
    String newMessage = request.getParameter("NewMessage");
    MaintForumGroupDetailForm dmsDocDiscussionForm = (MaintForumGroupDetailForm)form;
    ForumGroupDetail dmsDocDiscussion = (ForumGroupDetail) dmsDocDiscussionForm.getFormData();
    if (dmsDocDiscussion.getObjectID()==null){
      String objectID = request.getParameter("dummy_display_objectID");
      dmsDocDiscussion.setObjectID(TextUtility.parseIntegerObj(objectID));
    }
    Integer forumGroupDetailID = TextUtility.parseIntegerObj(dmsDocDiscussionForm.getID());
    ForumManager forumManager = new ForumManager(sessionContainer, conn);

    if (forumGroupDetailID==null) {
      log.debug("Create new forum message");
      String strForumType = request.getParameter("forumType");
      // Create New Topic of document discussion record
      if (Utility.isEmpty(dmsDocDiscussionForm.getAlwaysOnTop())) {
        dmsDocDiscussion.setAlwaysOnTop("N");
      }else {
        dmsDocDiscussion.setAlwaysOnTop("Y");
        }
        
      dmsDocDiscussion.setParentID(new Integer(0));
      dmsDocDiscussion.setReads(new Integer(0));
      dmsDocDiscussion.setRecordStatus(GlobalConstant.STATUS_ACTIVE);
      dmsDocDiscussion = forumManager.createForumGroupDetail(dmsDocDiscussion);
      ParentID=dmsDocDiscussion.getParentID();
    }else {
      log.debug("Create reply message");
      //Create Reply message based on the select Topic
      ForumGroupDetail masterDiscussionTopic = forumManager.getForumGroupDetail(forumGroupDetailID);

      //Update the Number of Read on Master Topic record
      int newRead = masterDiscussionTopic.getReads().intValue() + 1;
      masterDiscussionTopic.setReads(new Integer(newRead));
      ForumGroupDetail updatedDocDiscussion = forumManager.updateForumGroupDetail(masterDiscussionTopic);

      dmsDocDiscussion.setParentID(masterDiscussionTopic.getID());
      dmsDocDiscussion.setDetail(newMessage);
      dmsDocDiscussion.setReads(new Integer(0));
      dmsDocDiscussion.setRecordStatus(GlobalConstant.STATUS_ACTIVE);

      dmsDocDiscussion = forumManager.createForumGroupDetail(dmsDocDiscussion);
      ParentID=dmsDocDiscussion.getParentID();
    }


    dmsDocDiscussionForm.setFormData(dmsDocDiscussion);
    dmsDocDiscussionForm.setObjectID(TextUtility.formatIntegerObj(dmsDocDiscussion.getObjectID()));
    List replyList = new ArrayList();
    if (forumGroupDetailID!=null) { //For reply mode, select the corresponding reply list for display
      replyList = forumManager.getReplyList(ParentID, dmsDocDiscussion);
    }
    request.setAttribute("discussionReplyList", replyList);
  }

  public void updateRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {

    List resultList = new ArrayList();
    Integer ParentID;
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    MaintForumGroupDetailForm dmsDocDiscussionForm = (MaintForumGroupDetailForm)form;

    ForumGroupDetailDAObject dmsDocDiscussionDAO = new ForumGroupDetailDAObject(sessionContainer, conn);
    ForumGroupDetail newDmsDocDiscussion = (ForumGroupDetail) dmsDocDiscussionForm.getFormData();
    ForumGroupDetail dmsDocDiscussion = (ForumGroupDetail) dmsDocDiscussionDAO.getObjectByID(TextUtility.parseIntegerObj(dmsDocDiscussionForm.getID()));
    if (Utility.isEmpty(dmsDocDiscussionForm.getAlwaysOnTop())) {
      dmsDocDiscussion.setAlwaysOnTop("N");
    }else{
      dmsDocDiscussion.setAlwaysOnTop("Y");
    }
    //dmsDocDiscussion.setAlwaysOnTop(newDmsDocDiscussion.getAlwaysOnTop());
    dmsDocDiscussion.setTopic(newDmsDocDiscussion.getTopic());
    dmsDocDiscussion.setDetail(newDmsDocDiscussion.getDetail());

    ForumGroupDetail updatedDocDiscussion = (ForumGroupDetail) dmsDocDiscussionDAO.updateObject(dmsDocDiscussion);
    ParentID=dmsDocDiscussion.getParentID();


    dmsDocDiscussionForm.setFormData(dmsDocDiscussion);
    List replyList = new ArrayList();
    if (!"0".equals(dmsDocDiscussionForm.getParentID()) || "Y".equals(request.getParameter("showReplyList"))){
      replyList = dmsDocDiscussionDAO.getReplyList(ParentID.toString(), dmsDocDiscussion.getObjectType(), dmsDocDiscussion.getObjectID().toString());
    }else{
      replyList = dmsDocDiscussionDAO.getReplyList(dmsDocDiscussion.getID().toString(), dmsDocDiscussion.getObjectType(), dmsDocDiscussion.getObjectID().toString());
    
    }
    request.setAttribute("discussionReplyList", replyList);

  }

}